package com.situ.day4;

import org.junit.Test;

import java.util.Scanner;

public class ArrayDemo {

    @Test
    public void test1() {
        int score1 = 63;
        int score2 = 53;
        int score3 = 73;

        int[] array = new int[4];
        //数组访问：数组名[下标]
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        System.out.println(array.length);//4
        /*for (int i = 0; i <= array.length - 1; i++) {
            System.out.println(i);
        }*/
        for (int i = 0; i < array.length; i++) {
            System.out.println("i: " + i);
            System.out.println(array[i]);
        }
        System.out.println("----------------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test2() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;

        //在定义的时候直接赋值
        int[] array1 = {23, 45, 7, 9, 67};
        System.out.println(array1.length);//5
        int[] array2 = new int[]{23, 45, 7, 9, 67, 667};
        System.out.println(array2.length);//6
    }

    @Test
    public void test4() {
        int[] array = {23, 45, 7, 9};
        for (int i = 0; i <= array.length; i++) {
            //java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
            System.out.println(array[i]);
        }
    }

    @Test
    public void test114() {
        int[] array = {23, 45, 67, 2, 12};
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        System.out.println("sum: " + sum);
    }

    @Test
    public void test111() {
        int[] array = {23, 45, 67, 2, 12};
        //array.fori
        //初始认为0这个位置是最大的
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test112() {
        int[] array = {23, 56, 87, 2, 12};
        //方法调用：方法名(参数)
        int max = getMax(array);
        System.out.println("max: " + max);
    }

    @Test
    public void test153() {
        int[] array = {45, 67, 92, 12};
        //方法调用：方法名(参数)
        int max = getMax(array);
        System.out.println("max: " + max);
    }

    /**
     * 求最大值
     * @param array 要求最大值的数组
     * @return 返回数组最大值
     */
    public int getMax(int[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test113() {
        int[] array = {23, 45, 67, 2, 12};
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("min: " + min);
    }

    @Test
    public void test89() {
        //i=1:代表第一轮
        //i=1 j=3
        //i=2 j=2
        //i=3 j=1
        //i+j=array.length
        //j=array.length-i
        int[] array = {40, 17, 21, 1};
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test87() {
        int[] array = {40, 17, 21, 1};
        bubbleSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    private void bubbleSort(int[] array) {
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }


//    public void bubbleSort(int[] array) {
//
//    }

    @Test
    public void test189 () {
        int[][] array1 = {{1,2},{2,3},{3,4,5}};

        int[][] array = new int[3][];
        array[0] = new int[2];
        array[1] = new int[2];
        array[2] = new int[3];
        array[0][0] = 1;
        array[0][1] = 2;
        array[1][0] = 2;
        array[1][1] = 3;
        array[2][0] = 3;
        array[2][1] = 4;
        array[2][2] = 5;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }

    }

    @Test
    public void test34() {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        //String str = "abcba";
        //{'a','b','c','b','a'}
        char[] array = str.toCharArray();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test84() {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println(str);
        char ch = 'a';
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {

        }
        if (ch >= '0' && ch <= '9') {

        }
    }


}
